import Vue from 'vue'
import Router from 'vue-router'
import routes from 'src/router/routes'
import * as types from 'store/mutation-types'
import store from 'store'

Vue.use(Router)

let router = new Router({
    mode: 'hash',
    scrollBehavior(to, from, savedPosition) {
        return savedPosition || { x: 0, y: 0 }
    },
    routes
})

const setPageAnim = function(from, to) {
    var from_meta = from.meta || {},
        to_meta = to.meta || {}
    if (store.state.isBack) {
        store.commit(types.SET_PAGE_ANIMATE_TYPE, 'back')
        store.commit(types.UPDATE_BACK_STATUS, false)
    } else {
        if (from_meta.tab && to_meta.tab) {
            store.commit(types.SET_PAGE_ANIMATE_TYPE, 'tab')
        } else {
            store.commit(types.SET_PAGE_ANIMATE_TYPE, 'forward')
        }
    }
}

const LOGIN_CHECK_EXCLUDE_PATHS = ['/login', '/register'] // 登录检查排除的路径
router.beforeEach(function(to, from, next) {
    // if (utils.isLogin()) {
    //     if (to.path == '/login') { // 如果已经登录，不允许跳转到登录页面
    //         next(false)
    //     } else {
    //         setPageAnim(from, to)
    //         next()
    //     }
    //     return
    // }

    // if (LOGIN_CHECK_EXCLUDE_PATHS.indexOf(to.path) != -1) {
    //     setPageAnim(from, to)
    //     next()
    //     return
    // }

    // let route = {
    //     path: '/login',
    //     query: {
    //         from: to.path
    //     }
    // }
    // next(route)
    setPageAnim(from, to)
    next()
})

export default router